package com.lz.autotest.project.testmanage.infmanage.controller;

import com.lz.autotest.common.aspectj.lang.annotation.Log;
import com.lz.autotest.common.aspectj.lang.enums.BusinessType;
import com.lz.autotest.common.constant.UserConstants;
import com.lz.autotest.common.utils.SecurityUtils;
import com.lz.autotest.common.utils.StringUtils;
import com.lz.autotest.common.web.controller.BaseController;
import com.lz.autotest.common.web.domain.AjaxResult;
import com.lz.autotest.project.testmanage.infmanage.domain.SstInf;
import com.lz.autotest.project.testmanage.infmanage.service.InfManageService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 接口信息
 *
 * @author lz
 */
@RestController
@RequestMapping("/infManage")
public class InfManageController extends BaseController {
    @Resource
    private InfManageService infManageService;

    /**
     * 获取接口列表
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:list')")
    @GetMapping("/list")
    public AjaxResult list(SstInf sstInf) {
        List<SstInf> sstInfs = infManageService.selectSstInfList(sstInf);
        return AjaxResult.success(sstInfs);
    }

    /**
     * 获取接口列表,接口管理搜索框，模糊匹配名称和URL
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:list')")
    @GetMapping("/listByCondition")
    public AjaxResult listByCondition(SstInf sstInf) {
        List<SstInf> sstInfs = infManageService.selectSstInfListByCondition(sstInf);
        return AjaxResult.success(sstInfs);
    }

    @GetMapping("/queryInfAll")
    public Object queryInfAll(SstInf sstInf) {
        List stInfs = infManageService.queryInfAll(sstInf);
        return stInfs;
    }

    @GetMapping("/querySstInfAll")
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:all')")
    public Object querySstInfAll(@RequestParam Map<String, Object> map) {
        startPage();
        List stInfs = infManageService.querySstInfAll(map);
        return getDataTable(stInfs);
    }

    @GetMapping("/countInf")
    public Object countInf() {
        Map map = infManageService.countInf();
        return AjaxResult.success(map);
    }

    /**
     * 查询接口列表（排除节点）
     */

    @GetMapping("/list/exclude/{infId}")
    public AjaxResult excludeChild(@PathVariable(value = "infId", required = false) Long infId) {
        List<SstInf> sstInfs = infManageService.selectSstInfList(new SstInf());
        Iterator<SstInf> it = sstInfs.iterator();
        while (it.hasNext()) {
            SstInf d = (SstInf) it.next();
            if (d.getInfId().equals(infId)
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), infId)) {
                it.remove();
            }
        }
        return AjaxResult.success(sstInfs);
    }

    /**
     * 根据接口编号获取详细信息
     */
    @GetMapping(value = "/{infId}")
    public AjaxResult getInfo(@PathVariable String infId) {
        infManageService.checkSstInfDataScope(infId);
        return AjaxResult.success(infManageService.selectSstInfById(infId));
    }

    /**
     * 获取接口下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SstInf sstInf) {
        List<SstInf> sstInfs = infManageService.selectSstInfList(sstInf);
        return AjaxResult.success(infManageService.buildSstInfTreeSelect(sstInfs));
    }

    /**
     * 新增接口
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:add')")
    @Log(title = "接口管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SstInf sstInf) {
        sstInf.setCreateBy(SecurityUtils.getUserId().toString());
        infManageService.insertSstInf(sstInf);
        return AjaxResult.success(sstInf.getInfId());
    }

    /**
     * 修改接口
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:edit')")
    @Log(title = "接口管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SstInf sstInf) {
        String infId = sstInf.getInfId();
        infManageService.checkSstInfDataScope(infId);
        if (sstInf.getParentId().equals(infId)) {
            return AjaxResult.error("修改接口'" + sstInf.getInfName() + "'失败，上级接口不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, sstInf.getStatus()) && infManageService.selectNormalChildrenSstInfById(infId) > 0) {
            return AjaxResult.error("该接口包含未停用的子接口！");
        }
        sstInf.setUpdateBy(SecurityUtils.getUserId().toString());
        return toAjax(infManageService.updateSstInf(sstInf));
    }

    /**
     * 删除接口
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:remove')")
    @Log(title = "接口管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infId}")
    public AjaxResult remove(@PathVariable String infId) {
        if (infManageService.hasChildBySstInfId(infId)) {
            return AjaxResult.error("存在下级接口,不允许删除");
        }
        infManageService.checkSstInfDataScope(infId);
        return toAjax(infManageService.deleteSstInfById(infId));
    }

    /**
     * 复制接口
     */
    @PreAuthorize("@ss.hasPermi('testmanage:infManage:copy')")
    @Log(title = "接口管理", businessType = BusinessType.DELETE)
    @GetMapping("/copy/{infId}")
    public AjaxResult copyInf(@PathVariable String infId) {
        SstInf sstInf = infManageService.selectSstInfById(infId);
        if (sstInf == null) {
            return AjaxResult.error();
        }
        sstInf.setInfName(sstInf.getInfName() + " Copy");
        return toAjax(infManageService.insertSstInf(sstInf));
    }

    /**
     * 获取接口列表
     */
//    @PreAuthorize("@ss.hasPermi('testmanage:infManage:childrenList')")
    @Log(title = "接口管理", businessType = BusinessType.QUERY)
    @GetMapping("/childrenList/{infId}")
    public AjaxResult childrenList(@PathVariable String infId) {
        List<SstInf> list = infManageService.childrenList(infId);
        long end = System.currentTimeMillis();
        return AjaxResult.success(list);
    }


}
